package com.zudada.servermain.controller.admin;


import com.zudada.servermain.POJO.DTO.LoginDTO;
import com.zudada.servermain.POJO.VO.LoginVO;
import com.zudada.servermain.POJO.entity.PortUser;
import com.zudada.servermain.POJO.entity.User;
import com.zudada.servermain.POJO.enums.Result;
import com.zudada.servermain.Properties.JwtProperties;
import com.zudada.servermain.Service.LoginService;
import com.zudada.servermain.common.JwtUtil.JwtUtil;
import com.zudada.servermain.common.constant.JwtClaimsConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/admin")
@Slf4j
@Api(tags ="登陆接口")
public class LoginController {
    @Autowired
    private LoginService loginService;

    @Autowired

    private JwtProperties jwtProperties;



    /**
     * 登陆接口
     *
     * @param loginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation(value = "登陆接口")
    public Result login(@RequestBody LoginDTO loginDTO){
        log.info("人员登陆：{}",loginDTO);

        PortUser portUser = loginService.login(loginDTO);

        //登陆成功后，生成jwt令牌

        Map<String,Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.USER_ID,portUser.getUserId());

        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);
        User user = loginService.getRoleType(portUser.getUserId());


        LoginVO loginVO = LoginVO.builder()
                .userId(portUser.getUserId())
                .userName(portUser.getUsername())
                .name(portUser.getRealName())
                .roleType(user.getRoleType())
                .token(token)
                .build();

        return Result.success(loginVO);
    }

    /**
     * 退出
     * @return
     */
    @PostMapping("logout")
    @ApiOperation("人员退出")
    public Result<String> logout(){
        return Result.success();
    }



}
